Skip to content

Commit 66c6b91

Browse files
authored
fix: make logging handler close conditional to having the transport opened (#990)
There was a recent release (3.12.0) that included the changes introduced in #917. The newly introduced close method seems to be called by AppEngine Python runtime at shutdown, so if you would call it explicitly before the runtime does it, then the close function throws an exception because transport is None.
1 parent 5f89b5f commit 66c6b91

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

google/cloud/logging_v2/handlers/handlers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ def flush(self):
245245

246246
def close(self):
247247
"""Closes the log handler and cleans up all Transport objects used."""
248-
self.transport.close()
249-
self.transport = None
250-
self._transport_open = False
248+
if self._transport_open:
249+
self.transport.close()
250+
self.transport = None
251+
self._transport_open = False
251252

252253

253254
def _format_and_parse_message(record, formatter_handler):

tests/unit/handlers/test_handlers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,10 @@ def test_close(self):
901901
self.assertFalse(handler._transport_open)
902902
self.assertTrue(old_transport.close_called)
903903

904+
# second call to close shouldn't throw an exception
905+
handler.close()
906+
self.assertFalse(handler._transport_open)
907+
904908

905909
class TestFormatAndParseMessage(unittest.TestCase):
906910
def test_none(self):

0 commit comments

Comments
 (0)